home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / sprint2.c < prev    next >
C/C++ Source or Header  |  1999-11-29  |  15KB  |  479 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include "vidhrdw/generic.h"
  11.  
  12. /* machine/sprint2.c */
  13. extern int sprint2_collision1_data;
  14. extern int sprint2_collision2_data;
  15. extern int sprint2_gear1;
  16. extern int sprint2_gear2;
  17.  
  18. unsigned char *sprint2_horiz_ram;
  19. unsigned char *sprint2_vert_car_ram;
  20.  
  21. static struct osd_bitmap *back_vid;
  22. static struct osd_bitmap *grey_cars_vid;
  23. static struct osd_bitmap *black_car_vid;
  24. static struct osd_bitmap *white_car_vid;
  25.  
  26. #define WHITE_CAR   0
  27. #define BLACK_CAR   1
  28. #define GREY_CAR1   2
  29. #define GREY_CAR2   3
  30.  
  31. /***************************************************************************
  32. ***************************************************************************/
  33.  
  34. int sprint2_vh_start(void)
  35. {
  36.     if (generic_vh_start()!=0)
  37.         return 1;
  38.  
  39.     if ((back_vid = osd_create_bitmap(16,8)) == 0)
  40.     {
  41.         generic_vh_stop();
  42.         return 1;
  43.     }
  44.  
  45.     if ((grey_cars_vid = osd_create_bitmap(16,8)) == 0)
  46.     {
  47.         osd_free_bitmap(back_vid);
  48.         generic_vh_stop();
  49.         return 1;
  50.     }
  51.  
  52.     if ((black_car_vid = osd_create_bitmap(16,8)) == 0)
  53.     {
  54.         osd_free_bitmap(back_vid);
  55.         osd_free_bitmap(grey_cars_vid);
  56.         generic_vh_stop();
  57.         return 1;
  58.     }
  59.  
  60.     if ((white_car_vid = osd_create_bitmap(16,8)) == 0)
  61.     {
  62.         osd_free_bitmap(back_vid);
  63.         osd_free_bitmap(grey_cars_vid);
  64.         osd_free_bitmap(black_car_vid);
  65.         generic_vh_stop();
  66.         return 1;
  67.     }
  68.  
  69.     return 0;
  70. }
  71.  
  72. /***************************************************************************
  73. ***************************************************************************/
  74.  
  75. void sprint2_vh_stop(void)
  76. {
  77.     osd_free_bitmap(back_vid);
  78.     osd_free_bitmap(grey_cars_vid);
  79.     osd_free_bitmap(black_car_vid);
  80.     osd_free_bitmap(white_car_vid);
  81.     generic_vh_stop();
  82. }
  83.  
  84. /***************************************************************************
  85. sprint2_check_collision
  86.  
  87. It might seem strange to put the collision-checking routine in vidhrdw.
  88. However, the way Sprint2 hardware collision-checking works is by sending
  89. the video signals for the grey cars, white car, black car, black background,
  90. and white background through a series of logic gates.  This effectively checks
  91. for collisions at a pixel-by-pixel basis.  So we'll do the same thing, but
  92. with a little bit of smarts - there can only be collisions where the black
  93. car and white car are located, so we'll base our checks on these two locations.
  94.  
  95. We can't just check the color of the main bitmap at a given location, because one
  96. of our video signals might have overdrawn another one.  So here's what we do:
  97. 1)  Redraw the background, grey cars, black car, and white car into separate
  98. bitmaps, but clip to where the white car is located.
  99. 2)  Scan through the bitmaps, apply the logic from the logic gates and look
  100. for a collision (Collision1).
  101. 3)  Redraw the background, grey cars, black car, and white car into separate
  102. bitmaps, but clip to where the black car is located.
  103. 4)  Scan through the bitmaps, apply the logic from the logic gates, and look
  104. for a collision (Collision2).
  105. ***************************************************************************/
  106.  
  107. void sprint2_check_collision1(struct osd_bitmap *bitmap)
  108. {
  109.     int sx,sy,org_x,org_y;
  110.     struct rectangle clip;
  111.     int offs;
  112.  
  113.     clip.min_x=0;
  114.     clip.max_x=15;
  115.     clip.min_y=0;
  116.     clip.max_y=7;
  117.  
  118.     /* Clip in relation to the white car. */
  119.  
  120.     org_x=30*8-sprint2_horiz_ram[WHITE_CAR];
  121.     org_y=31*8-sprint2_vert_car_ram[WHITE_CAR*2];
  122.  
  123.     fillbitmap(back_vid,Machine->pens[1],&clip);
  124.     fillbitmap(grey_cars_vid,Machine->pens[1],&clip);
  125.     fillbitmap(white_car_vid,Machine->pens[1],&clip);
  126.     fillbitmap(black_car_vid,Machine->pens[1],&clip);
  127.  
  128.     /* Draw the background - a car can overlap up to 6 background squares. */
  129.     /* This could be optimized by not drawing all 6 every time. */
  130.  
  131.     offs=((org_y/8)*32) + ((org_x/8)%32);
  132.  
  133.     sx = 8 * (offs % 32)-org_x;
  134.     sy = 8 * (offs / 32)-org_y;
  135.  
  136.     drawgfx(back_vid,Machine->gfx[0],
  137.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  138.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  139.  
  140.     offs=((org_y/8)*32) + (((org_x+8)/8)%32);
  141.     sx = 8 * (offs % 32)-org_x;
  142.     sy = 8 * (offs / 32)-org_y;
  143.  
  144.     drawgfx(back_vid,Machine->gfx[0],
  145.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  146.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  147.  
  148.     offs=((org_y/8)*32) + (((org_x+16)/8)%32);
  149.     sx = 8 * (offs % 32)-org_x;
  150.     sy = 8 * (offs / 32)-org_y;
  151.  
  152.     drawgfx(back_vid,Machine->gfx[0],
  153.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  154.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  155.  
  156.     offs=(((org_y+8)/8)*32) + ((org_x/8)%32);
  157.     sx = 8 * (offs % 32)-org_x;
  158.     sy = 8 * (offs / 32)-org_y;
  159.  
  160.     drawgfx(back_vid,Machine->gfx[0],
  161.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  162.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  163.  
  164.     offs=(((org_y+8)/8)*32) + (((org_x+8)/8)%32);
  165.     sx = 8 * (offs % 32)-org_x;
  166.     sy = 8 * (offs / 32)-org_y;
  167.  
  168.     drawgfx(back_vid,Machine->gfx[0],
  169.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  170.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  171.  
  172.     offs=(((org_y+8)/8)*32) + (((org_x+16)/8)%32);
  173.     sx = 8 * (offs % 32)-org_x;
  174.     sy = 8 * (offs / 32)-org_y;
  175.  
  176.     drawgfx(back_vid,Machine->gfx[0],
  177.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  178.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  179.  
  180.  
  181.     /* Grey car 1 */
  182.     sx=30*8-sprint2_horiz_ram[GREY_CAR1];
  183.     sy=31*8-sprint2_vert_car_ram[GREY_CAR1*2];
  184.     sx=sx-org_x;
  185.     sy=sy-org_y;
  186.  
  187.     drawgfx(grey_cars_vid,Machine->gfx[1],
  188.             (sprint2_vert_car_ram[GREY_CAR1*2+1]>>3), GREY_CAR1,
  189.             0,0,sx,sy,&clip,TRANSPARENCY_NONE,0);
  190.  
  191.     /* Grey car 2 */
  192.     sx=30*8-sprint2_horiz_ram[GREY_CAR2];
  193.     sy=31*8-sprint2_vert_car_ram[GREY_CAR2*2];
  194.     sx=sx-org_x;
  195.     sy=sy-org_y;
  196.  
  197.     drawgfx(grey_cars_vid,Machine->gfx[1],
  198.             (sprint2_vert_car_ram[GREY_CAR2*2+1]>>3), GREY_CAR2,
  199.             0,0,sx,sy,&clip,TRANSPARENCY_COLOR,1);
  200.  
  201.  
  202.     /* Black car */
  203.     sx=30*8-sprint2_horiz_ram[BLACK_CAR];
  204.     sy=31*8-sprint2_vert_car_ram[BLACK_CAR*2];
  205.     sx=sx-org_x;
  206.     sy=sy-org_y;
  207.  
  208.     drawgfx(black_car_vid,Machine->gfx[1],
  209.             (sprint2_vert_car_ram[BLACK_CAR*2+1]>>3), BLACK_CAR,
  210.             0,0,sx,sy,&clip,TRANSPARENCY_NONE,0);
  211.  
  212.     /* White car */
  213.     drawgfx(white_car_vid,Machine->gfx[1],
  214.             (sprint2_vert_car_ram[WHITE_CAR*2+1]>>3), WHITE_CAR,
  215.             0,0,0,0,&clip,TRANSPARENCY_NONE,0);
  216.  
  217.     /* Now check for Collision1 */
  218.     for (sy=0;sy<8;sy++)
  219.     {
  220.         for (sx=0;sx<16;sx++)
  221.         {
  222.                 if (read_pixel(white_car_vid, sx, sy)==Machine->pens[3])
  223.                 {
  224.                     int back_pixel;
  225.  
  226.                     /* Condition 1 - white car = black car */
  227.                     if (read_pixel(black_car_vid, sx, sy)==Machine->pens[0])
  228.                         sprint2_collision1_data|=0x40;
  229.  
  230.                     /* Condition 2 - white car = grey cars */
  231.                     if (read_pixel(grey_cars_vid, sx, sy)==Machine->pens[2])
  232.                         sprint2_collision1_data|=0x40;
  233.  
  234.                     back_pixel = read_pixel(back_vid, sx, sy);
  235.  
  236.                     /* Condition 3 - white car = black playfield (oil) */
  237.                     if (back_pixel==Machine->pens[0])
  238.                         sprint2_collision1_data|=0x40;
  239.  
  240.                     /* Condition 4 - white car = white playfield (track) */
  241.                     if (back_pixel==Machine->pens[3])
  242.                         sprint2_collision1_data|=0x80;
  243.                }
  244.         }
  245.     }
  246.  
  247. }
  248.  
  249. void sprint2_check_collision2(struct osd_bitmap *bitmap)
  250. {
  251.  
  252.     int sx,sy,org_x,org_y;
  253.     struct rectangle clip;
  254.     int offs;
  255.  
  256.     clip.min_x=0;
  257.     clip.max_x=15;
  258.     clip.min_y=0;
  259.     clip.max_y=7;
  260.  
  261.     /* Clip in relation to the black car. */
  262.  
  263.     org_x=30*8-sprint2_horiz_ram[BLACK_CAR];
  264.     org_y=31*8-sprint2_vert_car_ram[BLACK_CAR*2];
  265.  
  266.     fillbitmap(back_vid,Machine->pens[1],&clip);
  267.     fillbitmap(grey_cars_vid,Machine->pens[1],&clip);
  268.     fillbitmap(white_car_vid,Machine->pens[1],&clip);
  269.     fillbitmap(black_car_vid,Machine->pens[1],&clip);
  270.  
  271.     /* Draw the background - a car can overlap up to 6 background squares. */
  272.     /* This could be optimized by not drawing all 6 every time. */
  273.  
  274.     offs=((org_y/8)*32) + ((org_x/8)%32);
  275.  
  276.     sx = 8 * (offs % 32)-org_x;
  277.     sy = 8 * (offs / 32)-org_y;
  278.  
  279.     drawgfx(back_vid,Machine->gfx[0],
  280.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  281.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  282.  
  283.     offs=((org_y/8)*32) + (((org_x+8)/8)%32);
  284.     sx = 8 * (offs % 32)-org_x;
  285.     sy = 8 * (offs / 32)-org_y;
  286.  
  287.     drawgfx(back_vid,Machine->gfx[0],
  288.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  289.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  290.  
  291.     offs=((org_y/8)*32) + (((org_x+16)/8)%32);
  292.     sx = 8 * (offs % 32)-org_x;
  293.     sy = 8 * (offs / 32)-org_y;
  294.  
  295.     drawgfx(back_vid,Machine->gfx[0],
  296.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  297.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  298.  
  299.     offs=(((org_y+8)/8)*32) + ((org_x/8)%32);
  300.     sx = 8 * (offs % 32)-org_x;
  301.     sy = 8 * (offs / 32)-org_y;
  302.  
  303.     drawgfx(back_vid,Machine->gfx[0],
  304.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  305.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  306.  
  307.     offs=(((org_y+8)/8)*32) + (((org_x+8)/8)%32);
  308.     sx = 8 * (offs % 32)-org_x;
  309.     sy = 8 * (offs / 32)-org_y;
  310.  
  311.     drawgfx(back_vid,Machine->gfx[0],
  312.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  313.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  314.  
  315.     offs=(((org_y+8)/8)*32) + (((org_x+16)/8)%32);
  316.     sx = 8 * (offs % 32)-org_x;
  317.     sy = 8 * (offs / 32)-org_y;
  318.  
  319.     drawgfx(back_vid,Machine->gfx[0],
  320.             videoram[offs] & 0x3F, (videoram[offs] & 0x80)>>7,
  321.             0,0,sx,sy, &clip,TRANSPARENCY_NONE,0);
  322.  
  323.  
  324.  
  325.  
  326.     /* Grey car 1 */
  327.     sx=30*8-sprint2_horiz_ram[GREY_CAR1];
  328.     sy=31*8-sprint2_vert_car_ram[GREY_CAR1*2];
  329.     sx=sx-org_x;
  330.     sy=sy-org_y;
  331.  
  332.     drawgfx(grey_cars_vid,Machine->gfx[1],
  333.             (sprint2_vert_car_ram[GREY_CAR1*2+1]>>3), GREY_CAR1,
  334.             0,0,sx,sy,&clip,TRANSPARENCY_NONE,0);
  335.  
  336.     /* Grey car 2 */
  337.     sx=30*8-sprint2_horiz_ram[GREY_CAR2];
  338.     sy=31*8-sprint2_vert_car_ram[GREY_CAR2*2];
  339.     sx=sx-org_x;
  340.     sy=sy-org_y;
  341.  
  342.     drawgfx(grey_cars_vid,Machine->gfx[1],
  343.             (sprint2_vert_car_ram[GREY_CAR2*2+1]>>3), GREY_CAR2,
  344.             0,0,sx,sy,&clip,TRANSPARENCY_COLOR,1);
  345.  
  346.  
  347.     /* White car */
  348.     sx=30*8-sprint2_horiz_ram[WHITE_CAR];
  349.     sy=31*8-sprint2_vert_car_ram[WHITE_CAR*2];
  350.     sx=sx-org_x;
  351.     sy=sy-org_y;
  352.  
  353.     drawgfx(white_car_vid,Machine->gfx[1],
  354.             (sprint2_vert_car_ram[WHITE_CAR*2+1]>>3), WHITE_CAR,
  355.             0,0,sx,sy,&clip,TRANSPARENCY_NONE,0);
  356.  
  357.     /* Black car */
  358.     drawgfx(black_car_vid,Machine->gfx[1],
  359.             (sprint2_vert_car_ram[BLACK_CAR*2+1]>>3), BLACK_CAR,
  360.             0,0,0,0,&clip,TRANSPARENCY_NONE,0);
  361.  
  362.     /* Now check for Collision2 */
  363.     for (sy=0;sy<8;sy++)
  364.     {
  365.         for (sx=0;sx<16;sx++)
  366.         {
  367.                 if (read_pixel(black_car_vid, sx, sy)==Machine->pens[0])
  368.                 {
  369.                     int back_pixel;
  370.  
  371.                     /* Condition 1 - black car = white car */
  372.                     if (read_pixel(white_car_vid, sx, sy)==Machine->pens[3])
  373.                         sprint2_collision2_data|=0x40;
  374.  
  375.                     /* Condition 2 - black car = grey cars */
  376.                     if (read_pixel(grey_cars_vid, sx, sy)==Machine->pens[2])
  377.                         sprint2_collision2_data|=0x40;
  378.  
  379.                     back_pixel = read_pixel(back_vid, sx, sy);
  380.  
  381.                     /* Condition 3 - black car = black playfield (oil) */
  382.                     if (back_pixel==Machine->pens[0])
  383.                         sprint2_collision2_data|=0x40;
  384.  
  385.                     /* Condition 4 - black car = white playfield (track) */
  386.                     if (back_pixel==Machine->pens[3])
  387.                         sprint2_collision2_data|=0x80;
  388.                }
  389.         }
  390.     }
  391. }
  392.  
  393. /***************************************************************************
  394.  
  395.   Draw the game screen in the given osd_bitmap.
  396.   Do NOT call osd_update_display() from this function, it will be called by
  397.   the main emulation engine.
  398.  
  399. ***************************************************************************/
  400. static void sprint_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  401. {
  402.     int offs,car;
  403.  
  404.     /* for every character in the Video RAM, check if it has been modified */
  405.     /* since last time and update it accordingly. */
  406.     for (offs = videoram_size - 1;offs >= 0;offs--)
  407.     {
  408.         if (dirtybuffer[offs])
  409.         {
  410.             int charcode;
  411.             int sx,sy;
  412.  
  413.             dirtybuffer[offs]=0;
  414.  
  415.             charcode = videoram[offs] & 0x3f;
  416.  
  417.             sx = 8 * (offs % 32);
  418.             sy = 8 * (offs / 32);
  419.             drawgfx(tmpbitmap,Machine->gfx[0],
  420.                     charcode, (videoram[offs] & 0x80)>>7,
  421.                     0,0,sx,sy,
  422.                     &Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  423.         }
  424.     }
  425.  
  426.     /* copy the character mapped graphics */
  427.     copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  428.  
  429.     /* Draw each one of our four cars */
  430.     for (car=3;car>=0;car--)
  431.     {
  432.         int sx,sy;
  433.  
  434.         sx=30*8-sprint2_horiz_ram[car];
  435.         sy=31*8-sprint2_vert_car_ram[car*2];
  436.  
  437.         drawgfx(bitmap,Machine->gfx[1],
  438.                 (sprint2_vert_car_ram[car*2+1]>>3), car,
  439.                 0,0,sx,sy,
  440.                 &Machine->drv->visible_area,TRANSPARENCY_COLOR,1);
  441.     }
  442.  
  443.     /* Refresh our collision detection buffers */
  444.     sprint2_check_collision1(bitmap);
  445.     sprint2_check_collision2(bitmap);
  446. }
  447.  
  448.  
  449. static void draw_gear_indicator(int gear, struct osd_bitmap *bitmap, int x, int color)
  450. {
  451.     /* gear shift indicators - not a part of the original game!!! */
  452.  
  453.     char gear_buf[6] = {0x07,0x05,0x01,0x12,0x00,0x00}; /* "GEAR  " */
  454.     int offs;
  455.  
  456.     gear_buf[5] = 0x30 + gear;
  457.     for (offs = 0; offs < 6; offs++)
  458.         drawgfx(bitmap,Machine->gfx[0],
  459.                 gear_buf[offs],color,
  460.                 0,0,(x+offs)*8,30*8,
  461.                 &Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  462. }
  463.  
  464.  
  465. void sprint2_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  466. {
  467.     sprint_vh_screenrefresh(bitmap,full_refresh);
  468.  
  469.     draw_gear_indicator(sprint2_gear1, bitmap, 25, 1);
  470.     draw_gear_indicator(sprint2_gear2, bitmap, 1 , 0);
  471. }
  472.  
  473. void sprint1_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  474. {
  475.     sprint_vh_screenrefresh(bitmap,full_refresh);
  476.  
  477.     draw_gear_indicator(sprint2_gear1, bitmap, 12, 1);
  478. }
  479.